package com.yjps.house.service;


import com.yjps.common.dao.AddressDao;
import com.yjps.common.model.Address;
import com.yjps.house.dao.HistoryDao;
import com.yjps.house.dao.HouseDao;
import com.yjps.house.model.House;
import com.yjps.house.model.PublishHistory;
import com.yjps.house.model.RecommendedHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("houseService")
public class HouseService {

    @Autowired
    private HouseDao houseDao;

    @Autowired
    private HistoryDao historyDao;

    @Autowired
    private AddressDao addressDao;

    /**
     * 存在以下情况不能修改房源信息：
     * 1.已认证
     * 2.未审核
     * 3.推荐(申请)
     * 4.无效的
     * @param house
     */
    @Transactional
    public boolean updatehouse(House house) {
        House localHouse = houseDao.get(house.getId());
        if (localHouse.getIsAuthentication() == 1 || localHouse.getState() == 0 ||
                localHouse.getIsBoutique() == 1 || localHouse.getIsValid() == 0) {
            return false;
        }
        houseDao.update(house);
        return true;
    }

    /**
     * 门店经理审核房源
     *
     * @param houseId
     * @param time
     * @param result
     * @return
     */
    @Transactional
    public boolean verfiyHouse(int houseId, String time, int result) {
        String hql="from t_house_publish_history p where p.house_id =: houseId";
        Map<String,Object> map=new HashMap<>();
        map.put("houseId",houseId);
        List<PublishHistory> phs=historyDao.find(hql, map);
        PublishHistory history=phs.get(0);

        //记录有效才能审核
        if (history.getIsValid()==0) {
            return false;
        }
        //更新记录
        history.setHandleResult(result);
        history.setTime(time);
        historyDao.update(history);
        //更新房源
        House house=houseDao.get(houseId);
        house.setState(result);
        updatehouse(house);
        return true;
    }


    /**
     * 门店经理确认是否为精品房源
     *
     * @param houseId
     * @param time
     * @param result
     * @return
     */
    @Transactional
    public boolean recommendHouse(int houseId, String time, int result) {
        String hql="from t_house_recommend_history p where p.house_id =: houseId";
        Map<String,Object> map=new HashMap<>();
        map.put("houseId",houseId);
        List<RecommendedHistory> rhs=historyDao.find(hql, map);
        RecommendedHistory rh=rhs.get(0);

        //记录有效才能审核
        if (rh.getIsValid()==0) {
            return false;
        }
        //更新记录
        rh.setHandleResult(result);
        rh.setTime(time);
        historyDao.update(rh);
        //更新房源
        House house=houseDao.get(houseId);
        house.setIsBoutique(result);
        updatehouse(house);
        return true;
    }

    /**
     * 根据条件查询房源：
     * 1.address
     *  {
     *      city：城市
     *      county：县区
     *      town：乡镇
     *  }
     * 2.house_area 面积
     * 3.house_sell_type 售出类型（卖或者租）
     * 4.house_type 房源类型
     * 5.house_is_boutique 是否精品
     * 6.house_authentication 是否认证
     * @param houseMap {house_area:"",house_sell_type:"",house_type:""，house_is_boutique:"",house_authentication:""}
     * @param addressMap
     * @return
     */
    public List<House> queryHouse( Map<String,Object> houseMap, Map<String,Object> addressMap,int startRow,int pageSize){
        //组合条件查询房源 除地址外
        String hql="from t_house where valid = 1 and house_state=1 ";
        for (String key : houseMap.keySet()) {
            if (!"".equals(houseMap.get(key))){
                hql=hql+"and "+key+" = "+houseMap.get(key);
            }
        }
        List<House> houseList=houseDao.find(hql);

        //匹配地址条件进行过滤
        for (House house:houseList){
            String hql2="from t_address a where a.address_id in " +
                    "(select b.address_id from t_building b where b.id =: buildingId)";
            Map<String,Object> map=new HashMap<>();
            map.put("buildingId",house.getBuildingId());
            Address address= (Address) addressDao.find(hql2,map).get(0);
            if(!compare(address,addressMap)){
                houseList.remove(house);
            }
        }
        return  houseList.subList(startRow-1,(startRow-1)*pageSize);
    }


    public boolean compare(Address address,Map<String,Object> addressMap){
        String cityCondition=(String)addressMap.get("city");
        String countyCondition=(String)addressMap.get("county");
        String townCondition=(String)addressMap.get("town");
        boolean flag=true;
        if (!"".equals(cityCondition)&&!address.getCity().equals(cityCondition)){
            flag=false;
        }
        if (flag&&!"".equals(cityCondition)&&!address.getCounty().equals(countyCondition)){
            flag=false;
        }
        if (flag&&!"".equals(townCondition)&&!address.getTown().equals(townCondition)){
            flag=false;
        }
       return flag;
    }
}